package com.abc.pay.client;

import java.util.*;

public class JSON {
    private String iJsonString;

    public JSON(String aJsonString) {
        this.iJsonString = aJsonString.trim();
    }

    public void setJsonString(String aJsonString) {
        this.iJsonString = aJsonString.trim();
    }

    public String getIJsonString() {
        return this.iJsonString;
    }

    public static String WriteDictionary(LinkedHashMap dic) {
        StringBuilder _output = new StringBuilder();
        boolean pendingSeparator = false;
        _output.append("{");
        Set<Map.Entry> entries = dic.entrySet();
        for(Iterator<Map.Entry> iter = entries.iterator(); iter.hasNext(); pendingSeparator = true) {
            Map.Entry entry = (Map.Entry)iter.next();
            if (pendingSeparator) {
                _output.append(",");
            }
            _output.append("\"");
            _output.append(entry.getKey());
            _output.append("\":");
            String value = (String)entry.getValue();
            if (value != null && value.startsWith("[{") && value.endsWith("}]")) {
                _output.append(value);
            } else {
                _output.append("\"");
                _output.append(value);
                _output.append("\"");
            }
        }
        return _output.toString();
    }

    public static String WriteDictionarys(LinkedHashMap dic) {
        StringBuilder _output = new StringBuilder();
        boolean pendingSeparator = false;
        _output.append("[");
        Set<Map.Entry> entries = dic.entrySet();
        for(Iterator<Map.Entry> iter = entries.iterator(); iter.hasNext(); pendingSeparator = true) {
            Map.Entry entry = iter.next();
            if (pendingSeparator) {
                _output.append("},");
            }
            String js = WriteDictionary((LinkedHashMap)entry.getValue());
            _output.append(js);
        }
        _output.append("}]");
        return _output.toString();
    }

    public static String ConvertHashMap2Json(LinkedHashMap dic) throws TrxException {
        StringBuilder _output = new StringBuilder();
        boolean pendingSeparator = false;
        _output.append("{");
        Set<Map.Entry> entries = dic.entrySet();
        for(Iterator<Map.Entry> iter = entries.iterator(); iter.hasNext(); pendingSeparator = true) {
            Map.Entry entry = iter.next();
            if (pendingSeparator) {
                _output.append(",");
            }
            _output.append("\"");
            _output.append(entry.getKey());
            _output.append("\":");
            String value;
            if (entry.getValue() instanceof LinkedHashMap) {
                value = ConvertHashMap2Json((LinkedHashMap)entry.getValue()) + "}";
                _output.append(value);
            } else {
                if (!(entry.getValue() instanceof String)) {
                    throw new TrxException("5000", "请求参数不合法 - " + entry.getValue());
                }
                value = (String)entry.getValue();
                if (value.startsWith("[{") && value.endsWith("}]")) {
                    _output.append(value);
                } else {
                    _output.append("\"").append(value).append("\"");
                }
            }
        }
        return _output.toString();
    }

    public String GetKeyValue(String aTag) {
        String json = this.iJsonString;
        int length;
        int index = json.indexOf(aTag);
        if (index == -1) {
            return "";
        } else {
            do {
                if (json.substring(index - 1, index).equals("\"") && json.substring(index + aTag.length(), index + aTag.length() + 1).equals("\"")) {
                    length = aTag.length();
                    index = index + length + 2;
                    String output = "";
                    char c = json.charAt(index);
                    if (c == '{') {
                        output = GetObjectValue(index, aTag, json);
                    }
                    if (c == '"') {
                        output = GetStringValue(index, aTag, json);
                    }
                    return output;
                }
                index = json.indexOf(aTag, index + 1);
            } while(index != -1);
            return "";
        }
    }

    private static String GetObjectValue(int index, String aTag, String json) {
        int count = 0;
        StringBuilder _output = new StringBuilder();
        while(true) {
            char c = json.charAt(index);
            if (c == '{') {
                ++count;
            }
            if (c == '}') {
                --count;
            }
            if (count == 0) {
                _output.append(c);
                return _output.toString();
            }
            _output.append(c);
            ++index;
        }
    }

    private static String GetStringValue(int index, String aTag, String json) {
        StringBuilder _output = new StringBuilder();
        ++index;
        while(true) {
            char c = json.charAt(index);
            if (c == '"') {
                return _output.toString();
            }
            _output.append(c);
            ++index;
        }
    }

    public LinkedHashMap GetArrayValue(String aTag) {
        String json = this.iJsonString;
        LinkedHashMap hashtableDictionary = new LinkedHashMap();
        Hashtable ht = new Hashtable();
        StringBuilder _output = new StringBuilder();
        int index = json.indexOf(aTag);
        if (index == -1) {
            return hashtableDictionary;
        } else {
            int length = aTag.length();
            index = index + length + 2;
            int key = 0;
            char c;
            do {
                do {
                    c = json.charAt(index++);
                } while(c == '[');
                if (c != ']') {
                    _output.append(c);
                }
                if (c == '}') {
                    c = json.charAt(index++);
                    ht.put(key, _output);
                    ++key;
                    _output = new StringBuilder();
                }
            } while(c != ']');
            if (ht.size() == 0) {
                return hashtableDictionary;
            } else {
                hashtableDictionary = ParseArray(ht);
                return hashtableDictionary;
            }
        }
    }

    public static LinkedHashMap ParseArray(Hashtable hts) {
        LinkedHashMap dic = new LinkedHashMap();
        String json;
        for(int i = 0; i < hts.size(); ++i) {
            json = hts.get(i).toString();
            Hashtable ht = ParseString(json);
            dic.put(i, ht);
        }
        return dic;
    }

    public static Hashtable ParseString(String json) {
        Hashtable ht = new Hashtable();
        int index = json.indexOf("{");
        int length = json.length();
        json = json.substring(index + 1, length - 1);
        json = json.replace("\"", "");
        String[] js = json.split(",");
        String[] key;
        for (String j : js) {
            key = j.split(":");
            if (1 == key.length) {
                ht.put(key[0], "");
            } else {
                ht.put(key[0], key[1]);
            }
        }
        return ht;
    }
}
